<?php
/**
 * User: 苏翔
 * Date: 2024/8/16 14:07
 */

namespace App\Dao;

use App\FundConf;
use App\HscoinConf;
use App\Model\FundCacheModel;
use App\Model\HscoinStatsModel;
use Hyperf\Di\Annotation\Inject;

class HscoinStatsDao extends BaseDao
{
    #[Inject]
    protected HscoinStatsModel $model;

    public function __construct()
    {
        $this->init();
    }

    function init(): bool
    {
        static $isInit = false;
        if (false === $isInit) {
            $isInit = $this->_init();
        }
        return $isInit;
    }

    function _init(): bool
    {
        $initList = [];
        $whereBalance = [
            'category' => 0,
            'cache_type' => HscoinConf::STATS_TYPE_BALANCE,
        ];
        $initList[] = $whereBalance;

        foreach (HscoinConf::$categoryExpenseLabelMap as $category => $label) {
            $initList[] = ['category' => $category, 'cache_type' => HscoinConf::STATS_TYPE_CATEGORY_EXPENSE];
        }

        foreach (HscoinConf::$categoryIncomeLabelMap as $category => $label) {
            $initList[] = ['category' => $category, 'cache_type' => HscoinConf::STATS_TYPE_CATEGORY_INCOME];
        }

        foreach ($initList as $item) {
            if (!$this->model->where($item)->value('id')) {
                $this->model->insertGetId($item);
            };
        }
        return true;
    }

    function getBalance()
    {
        return $this->model
            ->whereIn('cache_type',[HscoinConf::STATS_TYPE_BASE_BALANCE,HscoinConf::STATS_TYPE_BALANCE])
            ->sum('cnt');
    }
}